package com.bytech.user.biz.controller.blogUser;

import java.util.List;
import java.util.Set;
import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;
import com.bytech.common.annotation.UserAgent;
import com.bytech.common.biz.controller.base.BasicController;
import com.bytech.common.biz.entity.base.BaseResult;
import com.bytech.common.biz.entity.blogUser.BlogUser;
import com.bytech.common.biz.entity.blogUser.BlogUserAgent;
import com.bytech.common.biz.entity.blogUser.BlogUserLoginDto;
import com.bytech.common.config.AppGlobalConstants;
import com.bytech.common.utils.JedisClient;
import com.bytech.common.utils.StringUtils;
import com.bytech.user.biz.service.blogUser.BlogUserService;
import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;

/**
 * @Author Zhao
 * @Create 2021-07-26
 */
@RestController
@RequestMapping("/blogUser")
@Slf4j
public class BlogUserController extends BasicController {

    @Resource
    private BlogUserService blogUserService;
    @Resource
    private JedisClient jedisClient;

    @GetMapping("/whoAmI")
    public BaseResult<BlogUserAgent> whoAmI(@UserAgent BlogUserAgent userAgent) {
        return BaseResult.<BlogUserAgent>success().data(userAgent).build();
    }

    @GetMapping("/userDetail")
    public BaseResult<BlogUser> getBlogUserById(@RequestParam(value = "userId", required = false) String userId, @UserAgent BlogUserAgent userAgent) {
        BlogUser user = blogUserService.getById(userId);
        return BaseResult.<BlogUser>success().data(user).build();
    }

    @GetMapping("/getByIds")
    public List<BlogUser> getUserByIds(@RequestParam("userIds") Set<String> userIds) {
        if (CollectionUtils.isEmpty(userIds)) {
            return Lists.newArrayList();
        }
        return blogUserService.getByIds(userIds);
    }

    @GetMapping("/getByToken")
    public BaseResult<BlogUser> getBlogUserByToken(@RequestParam("token") String token) {
        if (StringUtils.isBlank(token)) {
            return BaseResult.<BlogUser>error("token不可为空").data(null).build();
        }
        log.info("finding user by token:" + token);
        return BaseResult.<BlogUser>success().data(blogUserService.getByToken(token)).build();
    }

    /**
     * 用户微服务对外提供的登录服务，管理后台也会通过这个接口登录
     * 拦截器会将会通过cookie中的token将登录状态记录在session中
     */
    @PostMapping("/login")
    public BaseResult<BlogUser> doLogin(@RequestBody BlogUserLoginDto loginDto) {
        if (StringUtils.isBlank(loginDto.getAccount()) || StringUtils.isBlank(loginDto.getPassword())) {
            return BaseResult.<BlogUser>error("账号密码不能为空").data(null).build();
        }
        return blogUserService.login(loginDto);
    }

    @PostMapping("/register")
    public BaseResult<BlogUser> doRegister(@RequestBody BlogUser blogUser) {
        return blogUserService.register(blogUser);
    }

    @PostMapping("/update")
    public BaseResult<BlogUser> doUpdate(@RequestBody BlogUser blogUser) {
        return blogUserService.modifyUser(blogUser);
    }

    @PostMapping("/updateByAgent")
    public BaseResult<BlogUser> doUpdateByAgent(@RequestBody BlogUserAgent blogUserAgent) {
        return blogUserService.modifyUserByAgent(blogUserAgent);
    }
}
